package view;

import controller.MyController;
import entity.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class Register extends JFrame implements ActionListener {
    JLabel[] label;
    JTextField[] t;
    JButton btn1,btn2;
    JPanel panel1,panel2,panel3,panel4;
    JPanel[] p;
    public Register(){
        super("用户注册");
        ImageIcon icon = new ImageIcon("src\\main\\resources\\img\\reg.png");
        this.setIconImage(icon.getImage());
        // 创建标签
        label = new JLabel[6];
        String[] l = {"登录名","姓   名","密   码","手机号","邮   箱","用户注册"};
        for (int i=0;i<6;i++){
            label[i]=new JLabel(l[i]);
        }
        // 创建输入框
        t = new JTextField[5];
        for (int i=0;i<5;i++){
            t[i] = new JTextField(20);
        }
        panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel1.add(label[5]);

        p = new JPanel[5];
        for (int i=0;i<5;i++){
            p[i] = new JPanel();
            p[i].setLayout(new FlowLayout());
            p[i].add(label[i]);
            p[i].add(t[i]);
        }

        panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());
        panel3 = new JPanel();
        panel3.setLayout(new BorderLayout());
        panel3.add(p[1],BorderLayout.NORTH);
        panel3.add(p[2],BorderLayout.CENTER);
        panel3.add(p[3],BorderLayout.SOUTH);
        panel2.add(p[0],BorderLayout.NORTH);
        panel2.add(panel3,BorderLayout.CENTER);
        panel2.add(p[4],BorderLayout.SOUTH);

        btn1 = new JButton("注册");
        btn2 = new JButton("重置");
        btn1.addActionListener(this);
        btn2.addActionListener(this);

        panel4 = new JPanel();
        panel4.setLayout(new FlowLayout());
        panel4.add(btn1);
        panel4.add(btn2);

        this.setLayout(new BorderLayout());
        this.add(panel1,BorderLayout.NORTH);
        this.add(panel2,BorderLayout.CENTER);
        this.add(panel4,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setResizable(false);
        this.setBounds(500,250,400,250);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn1){
            User user = new User();
            user.setLoginName(t[0].getText());
            user.setUserName(t[1].getText());
            user.setPassword(t[2].getText());
            user.setTelNumber(t[3].getText());
            user.setTelNumber(t[4].getText());
            boolean f = true;
            for (int i=0;i<5;i++){
                if (t[i].getText().equals("") || t[i].getText() == null){
                    f = false;
                    break;
                }
            }
            /**
             * 若f为true，说明输入框填写正常，否则说明有未填的输入框
             */
            if (f){
                try {
                    boolean flag = MyController.UserRegister(user);
                    if (flag){
                        JOptionPane.showMessageDialog(null, "注册成功！在登录页点击登录进入");
                        this.dispose();
                    }else {
                        JOptionPane.showMessageDialog(null, "用户名已存在！", "注册失败", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }else {
                JOptionPane.showMessageDialog(null, "用户注册格式有误，请修改信息后重新注册", "注册出错", JOptionPane.ERROR_MESSAGE);
            }
        }
        if (e.getSource() == btn2){
            for (int i=0;i<5;i++){
                t[i].setText(null);
            }
        }
    }
}
